//
//  GroupViewController.m
//  Topit
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "GroupViewController.h"
#import "VOSegmentedControl.h"
#import "YXYGroupHomePageModel.h"
#import "YXYGroupHotModel.h"
#import "YXYGroupHomePageCollectionViewCell.h"
#import "YXYGroupHotCollectionViewCell.h"
#import "YXYGroupNextViewController.h"
#import "YXYSearchsViewController.h"
#import "YXYLoginViewController.h"
@interface GroupViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, GroupNextURLDelegate>
@property (nonatomic, retain) UIView *myView;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) VOSegmentedControl *segmented;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, retain) NSMutableArray *hotArray;
@property (nonatomic, retain) NSDictionary *num;
@property (nonatomic, retain) NSDictionary *hotNum;
@property (nonatomic, assign) int number;
@end

@implementation GroupViewController
- (void)dealloc {
    [_hotNum release];
    [_num release];
    [_collectionView release];
    [_dataSource release];
    [_hotArray release];
    [_segmented release];
    [_myView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"小组";
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"groupman"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(groupManDidPress:)];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"signupmail"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(signupmailDidPress:)];
//    
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 88)];
    _myView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_myView];
    [_myView release];
    [self getDate];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.translucent = NO;
    [self setUpSearch];
    [self.collectionView reloadData];
}

#pragma mark - BarButtonItem
- (void)groupManDidPress:(UIBarButtonItem *)sender {
    YXYLoginViewController *loginVC = [[YXYLoginViewController alloc] init];
    [self.navigationController pushViewController:loginVC animated:YES];
}
- (void)signupmailDidPress:(UIBarButtonItem *)sender {
    
}
#pragma mark - UICollectionView
- (void)setUpCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.width, self.view.height - 132);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 88, self.view.width, self.view.height - 132) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    [_collectionView registerClass:[YXYGroupHomePageCollectionViewCell class] forCellWithReuseIdentifier:@"YXYGroupHomePageCollectionViewCellIdentifier"];
    
    [_collectionView registerClass:[YXYGroupHotCollectionViewCell class] forCellWithReuseIdentifier:@"YXYGroupHotCollectionViewCellIdentifier"];
    [self.view addSubview:_collectionView];
    [flowLayout release];
    [_collectionView release];
}

#pragma mark - collectionViewDataSource 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item == 0) {
        YXYGroupHomePageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YXYGroupHomePageCollectionViewCellIdentifier" forIndexPath:indexPath];
        [cell setDataSource:_dataSource];
        [cell setNum:_num];
        [cell.tableView reloadData];
        cell.nextDelegate = self;
        return cell;
    }else {
        YXYGroupHotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YXYGroupHotCollectionViewCellIdentifier" forIndexPath:indexPath];
        [cell setDataSource:_hotArray];
        [cell setNum:_hotNum];
        [cell.tableView reloadData];
        return cell;
    }

}
#pragma mark - getData 
- (void)getDate {
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载";
    hud.mode = MBProgressHUDModeIndeterminate;
    [YXYNetWorkTool getWithUrl:kManagerURL parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
      
        self.num = result[@"info"];
        for (NSDictionary *dic in result[@"item"]) {
            YXYGroupHomePageModel *groupHomePageModel = [[YXYGroupHomePageModel alloc] initWithDictionary:dic];
            [_dataSource addObject:groupHomePageModel];
            [groupHomePageModel release];
        }
        [hud hide:YES];
        dispatch_async(dispatch_get_main_queue(), ^{
            [_collectionView reloadData];

            [self setUpSegmented];
            
            [self setUpSearch];
            
            [self setUpCollectionView];
        });
        
    } fail:^(NSError *error) {
        [hud hide:YES];
        [ProgressHUD showError:@"请检查网络"];
    }];
    
    self.hotArray = [NSMutableArray arrayWithCapacity:0];
    [YXYNetWorkTool getWithUrl:kGroupHotURL parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        self.hotNum = result[@"info"];
        for (NSDictionary *dic in result[@"item"]) {
            YXYGroupHotModel *groupHotModel = [[YXYGroupHotModel alloc] initWithDictionary:dic];
            [_hotArray addObject:groupHotModel];
            [groupHotModel release];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [_collectionView reloadData];
        });
    } fail:^(NSError *error) {
        
    }];
}



#pragma mark - segmented 
- (void)setUpSegmented {
    self.segmented = [[VOSegmentedControl alloc] initWithSegments:@[@{VOSegmentText:@"热门小组"}, @{VOSegmentText:@"热门话题"}]];
    [_segmented release];
    _segmented.contentStyle = VOContentStyleTextAlone;
    _segmented.indicatorStyle = VOSegCtrlIndicatorStyleBottomLine;
    _segmented.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _segmented.selectedBackgroundColor = [UIColor groupTableViewBackgroundColor];
    _segmented.allowNoSelection = NO;
    _segmented.tag = 1000;
    [_segmented addTarget:self action:@selector(segmentDidPress:) forControlEvents:UIControlEventValueChanged];
    _segmented.frame = CGRectMake(0, 0, self.view.width, 44);
    _segmented.indicatorThickness = 4;
    [_myView addSubview:_segmented];
    [_segmented release];
    

}

- (void)segmentDidPress:(UISegmentedControl *)sender {
    [self.collectionView setContentOffset:CGPointMake(sender.selectedSegmentIndex * self.view.width, 0) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    UISegmentedControl *segmented = (UISegmentedControl *)[_myView viewWithTag:1000];
    if ([scrollView isEqual:self.collectionView]) {
        segmented.selectedSegmentIndex = self.collectionView.contentOffset.x / self.view.width;
    }
    
}
#pragma mark - search 
- (void)setUpSearch {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 45, self.view.width, 44)];
    view.layer.cornerRadius = 12;
    view.backgroundColor = [UIColor whiteColor];
    view.alpha = 0.5;
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 22, 22)];
    imageView.image = [UIImage imageNamed:@"search"];
    [view addSubview:imageView];
    [imageView release];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 10, 40, 22)];
    label.text = @"搜索";
    label.textColor = [UIColor grayColor];
    [view addSubview:label];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(5, 54, self.view.width - 10, 24);
    [button addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    [_myView addSubview:view];
    [_myView addSubview:button];
    [view release];
}
#pragma mark - 搜索
-(void)buttonDidPress:(UIButton *)sender {
    YXYSearchsViewController *searchVC = [[YXYSearchsViewController alloc] init];
    [self presentViewController:searchVC animated:YES completion:^{
    
    }];
    [searchVC release];
}

#pragma mark - 跳转
- (void)GroupDidPress:(NSString *)url {
    YXYGroupNextViewController *groupNextVC = [[YXYGroupNextViewController alloc]initWithURL:url];
    groupNextVC.view.backgroundColor = [UIColor whiteColor];
    CATransition *animation = [CATransition animation];
    animation.duration = 0.5f;
    [animation setType:@"suckEffect"];
    [self.navigationController.view.layer addAnimation:animation forKey:nil];
    [self.navigationController pushViewController:groupNextVC animated:YES];
    [groupNextVC release];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
